/**
 * 
 * 
 */
function Pipe() {

}
Pipe.prototype = {
	init: function() {
		this.pipes = {
			up: document.createElement('img'),
			down: document.createElement('img')
		}
		this.show().move();
		return this;
	},
	show: function() {
		this.pipes.up.className = this.pipes.down.className = "pipes";
		this.pipes.up.src = "img/pipe_down.png";
		var up_distance = randomUtils.Int(200, 400);
		this.pipes.up.style.bottom = up_distance + "px";
		this.pipes.down.src = "img/pipe_up.png";
		this.pipes.down.style.top = randomUtils.Int(500 - up_distance + 50, 500 - up_distance + 150) + "px";
		new GameEngine().main.appendChild(this.pipes.up);
		new GameEngine().main.appendChild(this.pipes.down);
		return this;
	},
	move: function() {
		var _this = this;
		this.pipes_intervar = setInterval(function() {
			_this.pipes.down.style.right = _this.pipes.up.style.right = getStyle(_this.pipes.up, 'right', false) + 6 + "px";
			if(_this.pipes.down.offsetLeft < -_this.pipes.down.offsetWidth) {
				_this.pipes.down.remove();
				_this.pipes.up.remove();
				clearInterval(_this.pipes_intervar);

			}
			_this.impact();
		}, 100);
	},
	impact: function() { //碰撞检测
		var bird = new GameEngine().bird.play_bird;
		var birdArea = {
			birdX: bird.offsetLeft,
			birdY: bird.offsetTop
		}
		
		var impactArea = {
			upleft: this.pipes.up.offsetLeft - bird.offsetWidth+10, //图片透明背景调整
			upright: this.pipes.up.offsetLeft + this.pipes.up.offsetWidth,
			upY: new GameEngine().main.offsetHeight - parseInt(this.pipes.up.style.bottom) -10,
			downY: this.pipes.down.offsetTop - bird.offsetHeight+35,
		}
		
		if(birdArea.birdX > impactArea.upleft && birdArea.birdX < impactArea.upright && (birdArea.birdY <impactArea.upY || birdArea.birdY > impactArea.downY)) {
			console.table(birdArea,impactArea);
			new GameEngine().stop();
		}
	}
}